package com.sduept.nwld.test;

import java.io.IOException;

/*import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.ErrorHandler;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;*/


public class SvgPngConverter {
	
    /**
     * batik通过读取svg文件的方式转png
     * @param filePath 传入读取的svg文件
     * @param pngFilePath 转换后的png图片
     * @param map 更改svg属性的集合 传值规则，id,name,value 主要是更改svg子节点的颜色属性值。
     * 						如果需要改变svg的多个element的颜色属性 则命名规范为 id1,name1,value1,id2,name2,value2....依次类推
     * @throws IOException
     * @throws TranscoderException
     */
    /*public static void convertToPngByFile(String filePath, String pngFilePath,Map<String, String> map)
			throws IOException, TranscoderException {
		File file = new File(pngFilePath);
		FileOutputStream outputStream = null;
		try {
			file.createNewFile();
			outputStream = new FileOutputStream(file);
			convertToPngByFile(filePath, outputStream,map);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
    
    public static void convertToPngByFile(String path, OutputStream outputStream,Map<String, String> map)
            throws TranscoderException, IOException {
        try {
        	File file = new File(path);
        	String parser = XMLResourceDescriptor.getXMLParserClassName();
			SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
			Document doc = f.createDocument(file.toURI().toString());
			if(map!=null && !map.isEmpty()){
				for (int i = 1; i <=map.size()/3; i++) {
					Element e = doc.getElementById(map.get("id"+i));
					log.info(map.get("name"+i));
					e.setAttribute(map.get("name"+i), map.get("value"+i));
				}
			}
            PNGTranscoder t = new PNGTranscoder();
            TranscoderInput input = new TranscoderInput(doc);
            TranscoderOutput output = new TranscoderOutput(outputStream);
            t.transcode(input, output);
            outputStream.flush();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    *//**
     * 将svg转换成png格式图片
     * 
     * @param pistrPngFile 图片路径 
     * @param svgPath      svg路径
     *//*
    public static  void changeSvgToJpg(String pistrPngFile, String pistrSvgPath) {
        Date date = new Date();
        long timeBegin = date.getTime();          
        // svg文件路径
        String strSvgURI;
        OutputStream ostream = null;
        try {
            // 根据路径获得文件夹
            File fileSvg = new File(pistrSvgPath);
            
            // 构造一个表示此抽象路径名的 file:URI
            URI uri = fileSvg.toURI();
            
            // 根据此 URI 构造一个 URL
            URL url = uri.toURL();
            
            // 构造此 URL 的字符串表示形式
            strSvgURI = url.toString();
            
            // 定义一个通用的转码器的输入
            TranscoderInput input = new TranscoderInput(strSvgURI);
            
            // 定义图片路径
            String strPngPath = pistrPngFile; 
            
            // 输入流
            ostream = new FileOutputStream(strPngPath);
            
            // 定义单路输出的转码器
            TranscoderOutput output = new TranscoderOutput(ostream);
             
            // 构造一个新的转码器，产生JPEG图像
            JPEGTranscoder transcoder = new JPEGTranscoder(); 
            
            // 设置一个转码过程，JPEGTranscoder.KEY_QUALITY设置输出png的画质精度，0-1之间
            transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));
	         transcoder.setErrorHandler(new ErrorHandler() {
				
				@Override
				public void warning(TranscoderException arg0) throws TranscoderException {
					log.info("warning");
					
				}
				
				@Override
				public void fatalError(TranscoderException arg0) throws TranscoderException {
					log.info("fatalError");
					
				}
				
				@Override
				public void error(TranscoderException arg0) throws TranscoderException {
					log.info("error");
					
				}
			});
            // 转换svg文件为png
            transcoder.transcode(input, output);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (TranscoderException e) {
            e.printStackTrace();
        } finally {
            try {
                ostream.flush();
                // 关闭输入流
                ostream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } 
        } 
        log.info("转换图片成功");
        // // 删除svg文件
        // fileSvg.delete();
        //log.info(pistrSvgPath + "已删除！");
        date = new Date();
        long timeEnd = date.getTime();
        long time = timeEnd - timeBegin;
        log.info("耗时" + time + "毫秒");
    }
    
    private static ArrayList<String> filelist = new ArrayList<String>(); 
    public static void main(String args[]){
    	try {
			convertToPngByFile("E:\\images\\5066549598093313.svg","E:\\images\\5066549598093313.png",null);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TranscoderException e) {
			e.printStackTrace();
		}
    	
    	 String filePath = "E:\\images\\bxSvg-sf\\bxSvg-sf";
    	 getFiles(filePath);
    	
    	//changeSvgToJpg("E:\\images\\png\\5066549603467265.png","E:\\images\\bxsvg\\bxsvg\\5066549603467265.svg");
    }
    
    
    static void getFiles(String filePath){
    	  File root = new File(filePath);
    	    File[] files = root.listFiles();
    	    for(File file:files){     
    	     if(file.isDirectory()){
    	      
    	       * 递归调用
    	       
    	      getFiles(file.getAbsolutePath());
    	      filelist.add(file.getAbsolutePath());
    	      log.info("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());
    	     }else{
    	      String pngName=file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1,file.getAbsolutePath().indexOf("."))+".png";
    	      log.info("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
    	      changeSvgToJpg("E:\\images\\png-sf\\"+pngName,file.getAbsolutePath());
    	     }     
    	  }
    }*/
}